//
//  XS_IntegralGetVC.swift
//  XueDao
//
//  Created by 范东 on 2020/12/25.
//

import UIKit

class XS_IntegralGetVC: XS_BaseVC {
    
    var leftTitleArray = ["完善信息","购买课程","邀请好友"]
    var imageArray = [#imageLiteral(resourceName: "icon_32_完善信息"),#imageLiteral(resourceName: "icon_32_购买课程"),#imageLiteral(resourceName: "icon_32_邀请好友")]
    var perfect = ("完善个人信息 学到币+"+(XS_Singleton.shared.settingModel?.perfect ?? "0"))
    var purchase = ("购买课程 学到币+"+(XS_Singleton.shared.settingModel?.purchase ?? "0"))
    var inviteXdMoney = ("邀请好友 学到币+"+(XS_Singleton.shared.settingModel?.inviteXdMoney ?? "0"))
    var contentArray : [String] = []
    var rightTitleArray = ["完善","购买","邀请"]

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        title = "获取学到币"
        contentArray = [self.perfect,self.purchase,self.inviteXdMoney]
        tableView.register(UINib.init(nibName: "XS_IntegralGetListCell", bundle: Bundle.main), forCellReuseIdentifier: "XS_IntegralGetListCell")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        XS_NetWork.requestJson("/setting/querySettings")
            .mapModel(type: XS_SettingModel.self) // 返回 模型对象 和原始数据
            .subscribe(onNext: { setModel, resp in
                XS_Singleton.shared.settingModel = setModel
                self.perfect = ("完善个人信息 学到币+"+(XS_Singleton.shared.settingModel?.perfect ?? "0"))
                self.purchase = ("购买课程 学到币+"+(XS_Singleton.shared.settingModel?.purchase ?? "0"))
                self.inviteXdMoney = ("邀请好友 学到币+"+(XS_Singleton.shared.settingModel?.inviteXdMoney ?? "0"))
                XS_NetWork.requestJson("/user/getCurrUser")
                    .mapModel(type: XS_UserBOM.self) // 返回 模型对象 和原始数据
                    .subscribe(onNext: { model, resp in
                        XS_Singleton.shared.infoModel?.userBO = model!
                        if model?.payCurriclumStatus == "complete"{
                            self.purchase = ("购买课程 学到币+"+(XS_Singleton.shared.settingModel?.purchase ?? "0"))
                        }
                        self.contentArray = [self.perfect,self.purchase,self.inviteXdMoney]
                        if model?.perfectStatus == "complete"{
                            if model?.payCurriclumStatus == "complete"{
                                self.rightTitleArray = ["已完善","已购买","去邀请"]
                            }else{
                                self.rightTitleArray = ["已完善","去购买","去邀请"]
                            }
                        }else{
                            if model?.payCurriclumStatus == "complete"{
                                self.rightTitleArray = ["去完善","已购买","去邀请"]
                            }else{
                                self.rightTitleArray = ["去完善","去购买","去邀请"]
                            }
                        }
                        self.tableView.reloadData()
                    }).disposed(by: self.disposeBag)
            }).disposed(by: disposeBag)
    }
}

extension XS_IntegralGetVC : UITableViewDelegate,UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return leftTitleArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XS_IntegralGetListCell", for: indexPath) as! XS_IntegralGetListCell
        cell.leftImageView.image = imageArray[indexPath.row]
        cell.titleLabel.text = leftTitleArray[indexPath.row]
        cell.contentLabel.text = contentArray[indexPath.row]
        cell.rightBtn.setTitle(rightTitleArray[indexPath.row], for: .normal)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        case 0:
            if XS_Singleton.shared.infoModel?.userBO.perfectStatus == "complete" {
                return
            }else{
                //完善信息
                let infoVC = XS_MineInfoVC()
                navigationController?.pushViewController(infoVC, animated: true)
            }
            break
        case 1:
            //购买课程
            if let tbc = XS_NVC.shared.viewControllers.first as? XS_TBC{
                tbc.selectedIndex = 0
            }
            navigationController?.popToRootViewController(animated: true)
            break
        case 2:
            //邀请好友
            let inviteVC = XS_InviteVC()
            navigationController?.pushViewController(inviteVC, animated: true)
            break
        default:
            break
        }
    }
}
